package com.lsk.admin.interceptor;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;
import com.lsk.util.ReturnData;
import org.apache.commons.lang.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * 根据代理商配置返利，配置面值折扣
 */
public class FaceValueInterceptor implements Interceptor {
    private Cache cache= Redis.use();

    @Override
    public void intercept(Invocation invocation) {
        String commodityId = invocation.getController().getPara("commodityId");
        String token = invocation.getController().getHeader("token");

        if (token!=null && "null".equals(token)==false ){

            if (StringUtils.isBlank(token))token=invocation.getController().getCookie("token");




            Map<String, Record> cacheMap = cache.get(token);

            if (cacheMap!=null){

                Record memberInfo = cacheMap.get("memberInfo");

                String sql="select a.id,a.commodityId,a.faceValue,a.number,a.saleCardDiscount,a.xkCommodityId,a.skCommodityId" +
                        ",if(b.rebatesThan is null,a.discount,b.rebatesThan) discount from commodity_facevalue a LEFT JOIN" +
                        " (select faceValueId,rebatesThan from yx_member_rebates where memberId='"+memberInfo.getStr("id")+"') b" +
                        " on a.id=b.faceValueId where a.commodityId='"+commodityId+"' ORDER BY a.faceValue";
                List<Record> list = Db.find(sql);

                ReturnData returnData = new ReturnData(true);
                returnData.setData(list);
                invocation.getController().renderJson(returnData);

            }




        }else {

            invocation.invoke();

        }

    }
}
